using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Gatsb.Application;
using Decav.Gatsb.Data;
using Decav.Gatsb;
using Decav.Gatsb.Web;

public partial class Post : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        commentErrorDiv.Visible = false;

        if (!IsPostBack)
        {
            SetupImages();
            InitializePage();
            SetupComments();
        }
    }

    private void InitializePage()
    {
        postCommentFormPanel.Visible = User.Identity.IsAuthenticated;
        notLoggedInDiv.Visible = !User.Identity.IsAuthenticated;

        locationLink.Location = CurrentPost.Location;
        headerTimeLiteral.Text = StringFormatting.ToTimeAgo(CurrentPost.CreateDate);

        postBody.Visible = (CurrentPost.Body != null && CurrentPost.Body.Trim().Length != 0);

        postBodyLiteral.Text = Server.HtmlEncode(CurrentPost.Body).Replace("\r\n", "<br/>");
        postingUserTile.User = CurrentPost.Owner;

        GatsbWebApplication.SetPageName(this, 
            string.Format("{0} at {1}, {2}",
            CurrentPost.Owner.Name.ToString(), CurrentPost.Location.Name, CurrentPost.CreateDate.ToShortDateString()));
    }

    private void SetupImages()
    {
        if (CurrentPost.Attachments.Count == 0)
        {
            postImage.Visible = false;
            imageRepeater.Visible = false;
        }
        else
        {
            postImage.Visible = true;
            imageRepeater.Visible = true;
            imageRepeater.DataSource = CurrentPost.Attachments;
            imageRepeater.DataBind();

            postImage.Src = GatsbApplication.Current.GetImageUrl(CurrentPost.Attachments[0].Id, UserImageSize.Original);
        }
    }

    private void SetupComments()
    {
        PostComment[] comments = null;
        using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
        {
            comments = adapt.GetPostComments(Id);
        }

        commentsRepeater.DataSource = comments;
        commentsRepeater.DataBind();
    }

    protected void GotoLogin_Click(object sender, EventArgs e)
    {
        FormsAuthentication.RedirectToLoginPage();
    }

    protected void SubmitCommentButton_Click(object sender, EventArgs e)
    {
        if (commentTextBox.Text.Trim().Length == 0)
        {
            commentErrorDiv.Visible = true;
            return;
        }

        GatsbApplication.Current.AddPostComment(new PostComment(Id, commentTextBox.Text));

        commentTextBox.Text = string.Empty;
        SetupComments();
    }

    /// <summary>
    /// Gets the unique identifier of the post to get.
    /// </summary>
    public Guid Id
    {
        get
        {
            if (Request.QueryString["Id"] == null)
                throw new ArgumentNullException("Id");

            return new Guid(Request.QueryString["Id"]);
        }
    }

    private Decav.Gatsb.Post _post;

    /// <summary>
    /// Gets the post that this page renders.
    /// </summary>
    public Decav.Gatsb.Post CurrentPost
    {
        get
        {
            try
            {
                if (_post == null)
                {
                    using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
                        _post = adapt.GetPost(Id);
                }
            }
            catch (DataAccessDeniedException)
            {
                Response.Redirect("~/AccessDeniedMessage.aspx", true);
            }

            return _post;
        }
    }

    protected bool UserCanEditComment(Guid commentOwnerId)
    {
        return HttpContext.Current.User.Identity.IsAuthenticated &&
            (commentOwnerId == GatsbApplication.Current.CurrentUser.Id ||
            GatsbApplication.Current.CurrentUser.Id == CurrentPost.Owner.Id);

    }

    protected void CommentItem_Command(object sender, RepeaterCommandEventArgs e)
    {
        Guid commentId = new Guid((string)e.CommandArgument);

        using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
        {
            adapt.DeleteComment(commentId);
        }

        SetupComments();
    }
}
